package see.battle.object;

import java.util.LinkedList;
import java.util.Random;
import see.battle.Const;
import see.battle.drawer.Drawer;

public class ShipContainer {
  
  private LinkedList<Ship> _ships = new LinkedList<Ship>();
  private int _direction;
  private Random _rand;
  private static final long serialVersionUID = 1L;

 /**
   * @param direction
   */
  public ShipContainer(int direction) {
    _direction = direction;
  }
  
  public int get_direction() {
	  return _direction;
  }
  
  public int getShipsNumber() {
    return _ships.size();
  }
  
  public void initRandom(long seed) {
    _rand = new Random(seed);
  }

  public void changeDirection() {
    _direction = -_direction;
    for (Ship e : _ships) {
      e.set_direction(_direction);
    }
  }
  
  public void stopShipsForBlast() {
    for (Ship e : _ships) {
      e.stopForBlast();
    }
  }
  
  public void addShip(Drawer drawer, Background bg, long creationTime, 
      long currentTime) {
    int n = _rand.nextInt(Const.shipTypesNumber);
    if (_direction == Const.fromLeftToRightDirection) {
      _ships.addFirst(ShipFactory.makeShip(drawer, n, _direction, bg,  
          creationTime, currentTime));
    } else {
      _ships.addLast(ShipFactory.makeShip(drawer, n, _direction, bg,  
          creationTime, currentTime));
    }
  }

  private void tryToRemoveDisapperedShip() {
    if (_ships.size() != 0) {
      if (_direction == Const.fromLeftToRightDirection) {
        if (_ships.getLast().get_position() > Const.backgroundRight) {
          _ships.removeLast(); 
        }
      } else {
        if (_ships.getFirst().get_position() < 
            Const.backgroundLeft - Const.shipWidth) {
          _ships.removeFirst();
        }
      }
    }
  }
  
  public void updateShips(long currentTime) {
    for (Ship e : _ships) {
      e.update(currentTime);
    }
    tryToRemoveDisapperedShip();
  }
  
  public void drawShips() { 
    for (Ship e : _ships) {
      e.draw();
    }
  }
  
  public long proceedBullet(float position) {
    boolean hit = false;
    float leftFutureShipPosition = Const.backgroundRight;
    float rightFutureShipPosition = Const.backgroundLeft;
    long tillHitDelay = (long)(((float)1.0 - Const.backgroundSeaLevel) 
        / Const.bulletSpeed * Const.updateDelay);
    float distance = tillHitDelay * Const.shipSpeed / Const.updateDelay;
    for (Ship e : _ships) {
      float futureShipPosition = e.get_position() 
      + distance * e.get_direction();
      if (futureShipPosition > Const.backgroundLeft
          - Const.shipWidth) {
        leftFutureShipPosition = Math.min(leftFutureShipPosition, 
            futureShipPosition);
      }
      if (futureShipPosition < Const.backgroundRight) {
        rightFutureShipPosition = Math.max(rightFutureShipPosition, 
            futureShipPosition);
      }
      if ((position > futureShipPosition) && 
          (position < futureShipPosition + Const.shipWidth)) {
        hit = true;
      }
    }
    if (hit) {
      long additionalShipDelay = 0; 
      if (_direction == Const.fromRightToLeftDirection) {
        float desiredLeftShipPosition = Const.backgroundLeft
            - Const.shipWidth
            + Const.shipSpeed * (float)(Const.shipDelay / Const.updateDelay);
        additionalShipDelay = (long)((desiredLeftShipPosition 
            - leftFutureShipPosition) / Const.shipSpeed 
            * (float)Const.updateDelay);
      } else {
        float desiredRightShipPosition = Const.backgroundRight
            - Const.shipSpeed * (float)(Const.shipDelay / Const.updateDelay);
        additionalShipDelay = (long)((rightFutureShipPosition 
            - desiredRightShipPosition) / Const.shipSpeed 
            * (float)Const.updateDelay);
      }
      return additionalShipDelay;
    } else {
      return -1;
    }
  }  
 
}
